<?php
$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('PublicHelper');
$PublicHelper = new PublicHelper();

$this->LoadHelper('StretchHelper');
$StretchHelper = new StretchHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if(!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

if (empty($this->Param['option'])) {
    $member_info = $MemberHelper->getMember($this->Param['uid']);
    $status = $this->Param['status'];
    //待征信查询列表
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    
    $where = array(
        '`o_status` != ?' => '作废'
    );
    if(!empty($status)){
        if($status == 99){
            $where['`o_status` = ? OR `o_quit_status` != ""'] = '退回';
        }else{
            $where['`o_status` = ?'] = $OrderHelper->statusChange($status);
        }
        
    }
    $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info);
    $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal']);
    if (!empty($data['all'])){
        foreach ($data['all'] AS $rowNum => $row) {
            $cert_row = $OrderHelper->certRow(array('`ct_oid` = ?' => $row['o_id'], '`ct_status` = ?' => 'ok'));
            $row['ct_id'] = $cert_row['ct_id'];
            $row['o_status_code'] = $OrderHelper->statusChange($row['o_status']);
            
            $stretch_row = $StretchHelper->signRow(array('`sr_oid` = ?' => $row['o_id'], '`sr_result` != ?' => 'del'));
            $row['stretch_check'] = $StretchHelper->signJudge($row['o_id']);
            if(!empty($stretch_row)){
                $row['stretch_status'] = $stretch_row['sr_result'];
                $row['stretch_idea'] = $stretch_row['sr_idea'];
                $row['stretch_uname'] = $stretch_row['sr_deal_uname'];
                $row['stretch_dateline'] = $stretch_row['sr_deal_time'];
            }
            $audit = $OrderHelper->auditRow(array('`a_oid` = ?' => $row['o_id'], '`a_status` = ?' => 'ok', '`a_results` = ?' => '退单'));
            $row['back_user'] = $audit['a_uname'];
            $row['back_reason'] = $audit['a_details'];
            $json_data['list'][] = $row;
        }
    }
    $ApiHelper->logSave('orderindex', $this->Param);
    ErrorMsg::ApiMsg('请求成功', 0, $json_data);
    
} else {
    switch ($this->Param['option']){
        case 'info':
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            $order = $OrderHelper->orderInfo($oid);
            if(empty($order['owner']['ot_contact'])){
                $order['owner']['ot_contact'] = array();
            }else{
                foreach($order['owner']['ot_contact'] as $k => $v){
                    if(empty($v)) $order['owner']['ot_contact'][$k] = '';
                }
            }
            $order['spouse']['ct_detail'] = empty($order['spouse']['ct_detail']) ? '' : $order['spouse']['ct_detail'];
            if(empty($order['guarantor'])) $order['guarantor'] = array();
            if(empty($order['car'])) $order['car'] = array();
            if(empty($order['car']['ot_calculated'])) $order['car']['ot_calculated'] = '';
            if(empty($order['attachment'])){
                $order['attachment'] = array();
                $order['attachment']['oa_video_name'] = array();
            }else{
                foreach($order['attachment'] as $k => $v){
                    if(empty($v)){
                        $order['attachment'][$k] = array();
                    }
                    if($k=='oa_video'){
                        $order['attachment']['oa_video_name'] = array();
                        foreach($order['attachment'][$k] as $k1 => $v1){
                             $order['attachment']['oa_video_name'][] = wodrow\models\AttachFile::getFileNameByPath($v1);
                        }
                    }
                    /*if ($k = 'oa_signalcard'&&$order['attachment']['oa_signalcard']){
                        if (!is_array($order['attachment']['oa_signalcard'])){
                            $order['attachment']['oa_signalcard'] = \wodrow\tools\ArrayHelp::str2arr($order['attachment']['oa_signalcard']);
                        }
                        return $order['attachment']['oa_signalcard'];
                    }*/
                }
            }
            if(!empty($order['guarantor'])){
                $x = 'a:30:{s:15:"ct_guarantor_id";s:2:"12";s:21:"ct_guarantor_province";s:9:"湖北省";s:17:"ct_guarantor_city";s:9:"十堰市";s:17:"ct_guarantor_area";s:6:"郧县";s:19:"ct_guarantor_degree";s:6:"大专";s:19:"ct_guarantor_mobile";s:11:"15071602518";s:20:"ct_guarantor_married";s:12:"离异单身";s:20:"ct_guarantor_liveadd";s:39:"湖北省郧县城关镇兴郧路10号 ";s:16:"ct_guarantor_tel";s:1:"0";s:17:"ct_guarantor_hadd";s:39:"湖北省郧县城关镇兴郧路10号 ";s:18:"ct_guarantor_headd";s:39:"湖北省郧县城关镇兴郧路10号 ";s:19:"ct_guarantor_howner";s:9:"刘瑞斌";s:22:"ct_guarantor_hrelation";s:6:"朋友";s:20:"ct_guarantor_hnature";s:9:"自建房";s:21:"ct_guarantor_hacreage";s:1:"0";s:19:"ct_guarantor_hprice";s:1:"0";s:18:"ct_guarantor_hloan";s:1:"0";s:20:"ct_guarantor_hloyear";s:1:"0";s:22:"ct_guarantor_hrefundmo";s:1:"0";s:20:"ct_guarantor_company";s:1:"0";s:17:"ct_guarantor_cadd";s:1:"0";s:17:"ct_guarantor_ctel";s:1:"0";s:18:"ct_guarantor_ctype";s:6:"其它";s:19:"ct_guarantor_dutytp";s:6:"其他";s:17:"ct_guarantor_duty";s:6:"其它";s:21:"ct_guarantor_operatpd";s:3:"无";s:22:"ct_guarantor_seniority";s:3:"无";s:18:"ct_guarantor_stake";s:1:"0";s:20:"ct_guarantor_mincome";s:1:"0";s:19:"ct_guarantor_remark";s:2:"00";}';
                $x = unserialize($x);
                $y = new stdClass();
                foreach ($x as $k => $v){
                    $y->$k = "";
                }
                foreach($order['guarantor'] as $k => $v){
                    $order['guarantor'][$k]['ct_detail'] = empty($v['ct_detail']) ? $y : $v['ct_detail'];
                    $order['guarantor'][$k]['ct_video'] = empty($v['ct_video']) ? array() : unserialize($v['ct_video']);
                    $order['guarantor'][$k]['ct_sign'] = empty($v['ct_sign']) ? array() : unserialize($v['ct_sign']);
                    $order['guarantor'][$k]['ct_attach'] = empty($v['ct_attach']) ? \wodrow\tools\Obj::getAttach() : unserialize($v['ct_attach']);
                }
            }
            ErrorMsg::ApiMsg('请求成功', 0, $order);
            break;
    }
}